package com.example.administrator.view;

import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.example.administrator.base.BaseActivity;
import com.example.administrator.bean.LoginStatusBean;
import com.example.administrator.module.UrlDefine;
import com.example.administrator.testapk.R;
import com.example.administrator.utils.JsonUtils;
import com.example.administrator.utils.NetUtils;
import com.example.administrator.utils.SpUtils;

import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ConcurrentSkipListMap;

import okhttp3.Call;
import okhttp3.Response;

public class AtyWelcome extends BaseActivity {

    private static final int TIME = 3000;
    private static final int GO_LOGIN = 100;
    private static final int GO_MAIN = 101;
    private static final String TAG = "AtyWelcome";
    public String phone;
    public String pwd;

    Handler mhandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case GO_MAIN:
                    goMain();
                    break;
                case GO_LOGIN:
                    goLogin();
                    break;
            }
        }
    };

    private void goLogin() {
        forward(AtyLogin.class);
    }

    private void goMain() {
        Map<String,String> reqbody = new ConcurrentSkipListMap<>();
        reqbody.put("loginName", phone);
        reqbody.put("password", pwd);

        NetUtils.getInstance().postDataAsynToNet(UrlDefine.ACTION_URL_LOGIN, reqbody, new NetUtils.MyNetCall() {
            @Override
            public void success(Call call, Response response) throws IOException {
                final String loginStatus = response.body().string();
                Log.e("loginStatus",loginStatus);
                final LoginStatusBean loginStatusBean = JsonUtils.JsonStr2LoginStatusBean(loginStatus);
                Log.d(TAG,loginStatusBean.getStatus() +"");
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if(loginStatusBean.getStatus() == 200){
                            forward(AtyMain.class);
                        }else{
                            //toast(loginStatusBean.getMsg());
                            forward(AtyLogin.class);
                        }
                    }
                });
            }

            @Override
            public void failed(Call call, IOException e) {
                toast(e.getMessage());
            }
        });
    }

    @Override
    protected int getLayoutResourceId() {
        return R.layout.aty_welcome;
    }

    @Override
    protected void init() {
         phone = (String) SpUtils.get(AtyWelcome.this,"phone","");
         pwd = (String)SpUtils.get(AtyWelcome.this,"pwd","");
    }

    @Override
    protected void initView() {
        if(phone!=null && pwd!=null){
            mhandler.sendEmptyMessageDelayed(GO_MAIN, TIME);
        }
    }
}